//
//  NavViewController.m
//  newTestGit
//
//  Created by fangjs on 16/3/11.
//  Copyright © 2016年 Hongshi. All rights reserved.
//

#import "NavViewController.h"
#import <MapKit/MapKit.h>
@interface NavViewController ()<MKMapViewDelegate>
@property (strong, nonatomic)  MKMapView *mapView;
@property (nonatomic , strong) CLGeocoder *geoC;

@property (nonatomic , strong) UIButton *btn;
@property (nonatomic , strong) UIButton *btn1;
@end

@implementation NavViewController


-(CLGeocoder *)geoC {
    if (_geoC == nil) {
        _geoC = [[CLGeocoder alloc] init];
    }
    return _geoC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *ietm = [[UIBarButtonItem alloc] initWithTitle:@"返回" style: UIBarButtonItemStylePlain target:self action:@selector(itemAction)];
    self.navigationItem.leftBarButtonItem = ietm;

    
    self.btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.btn.tag = 200;
    self.btn.backgroundColor = [UIColor orangeColor];
    [self.btn setTitle:@"开始导航" forState:UIControlStateNormal];
    [self.btn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btn];
    
    self.btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.btn1.tag = 201;
    self.btn1.backgroundColor = [UIColor orangeColor];
    [self.btn1 setTitle:@"屏幕截图" forState:UIControlStateNormal];
    [self.btn1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btn1];
 
    [self updateViewConstraints];
}

- (void) updateViewConstraints {
    [super updateViewConstraints];
    
    [self.btn autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:self.view.bounds.size.height/3];
    [self.btn autoAlignAxisToSuperviewAxis:ALAxisVertical];
    [self.btn autoSetDimensionsToSize:CGSizeMake(200, 44)];
    
    [self.btn1 autoAlignAxisToSuperviewAxis:ALAxisVertical];
    [self.btn1 autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.btn withOffset:30];
    [self.btn1 autoSetDimensionsToSize:CGSizeMake(200, 44)];

}

- (void) itemAction {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void) buttonAction:(UIButton *) btn {
    if (btn.tag == 200) {
        [self initNavigationAddress];
    }
    else {
        _mapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        [self ScreenCapture];
    }
}

- (void) touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

}

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
}

//初始化导航地址
- (void) initNavigationAddress {
    self.view.backgroundColor = [UIColor whiteColor];
    [self.geoC geocodeAddressString:@"杭州" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        CLPlacemark *hzP = [placemarks firstObject];
        [self.geoC geocodeAddressString:@"上海" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            
            CLPlacemark *shP = [placemarks firstObject];
            [self beginNavWithBeginPlace:hzP andEndPlace:shP];
        }];
    }];

}

//屏幕截图
- (void) ScreenCapture {
    //地图快照截图
    MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
    //针对地图
    options.region = self.mapView.region;
    options.showsBuildings = YES;
    
    //针对输出图片
    options.size = CGSizeMake(1000, 2000);
    options.scale = [UIScreen mainScreen].scale;
    
    MKMapSnapshotter *snap = [[MKMapSnapshotter alloc] initWithOptions:options];
    [snap startWithCompletionHandler:^(MKMapSnapshot * _Nullable snapshot, NSError * _Nullable error) {
        if (error == nil) {
            UIImage *image = snapshot.image;
            NSData *data = UIImagePNGRepresentation(image);//将 image转化为 NSData
            [data writeToFile:@"/Users/wujue/Desktop/map.png" atomically:YES];
            NSLog(@"存储成功");
        }
        else {
            NSLog(@"--%@", error.localizedDescription);
        }
    }];
}

- (void) beginNavWithBeginPlace:(CLPlacemark *) beginPlace andEndPlace:(CLPlacemark *) endPlace {
    //创建开始的地图项
    CLPlacemark *cPlacemarkB = beginPlace;
    MKPlacemark *mPlacemarkB = [[MKPlacemark alloc] initWithPlacemark:cPlacemarkB];
    MKMapItem *mapItemBegin = [[MKMapItem alloc] initWithPlacemark:mPlacemarkB];
    
    //创建结束的地图项
    CLPlacemark *cPlacemarkE = endPlace;
    MKPlacemark *mPlacemarkE = [[MKPlacemark alloc] initWithPlacemark:cPlacemarkE];
    MKMapItem *mapItemEnd = [[MKMapItem alloc] initWithPlacemark:mPlacemarkE];

    NSArray *items = @[mapItemBegin,mapItemEnd];
    NSDictionary *dic = @{
                          MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
                          MKLaunchOptionsMapTypeKey:@(MKMapTypeHybrid),
                          MKLaunchOptionsShowsTrafficKey:@(YES)
                          };
    
    [MKMapItem openMapsWithItems:items launchOptions:dic];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
